import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';

import '../../net/response/office_list_tree_data.dart';
import '../../res/gaps.dart';
import '../../viewmodel/office_list_view_model.dart';

class OfficeListPage extends BaseStatefulMvvmWidget {
  @override
  State<StatefulWidget> createState() => _OfficeListState();
}

class _OfficeListState
    extends BaseMvvmState<OfficeListViewModel, OfficeListPage> {
  @override
  Widget buildLoadingContentView() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [

        // Column(
        //   children: List.generate(viewModel.selectOffice.length, (index) {
        //      var item =  viewModel.selectOffice[index];
        //     return Row(
        //       children: [
        //         Column(
        //           children: [
        //             Container(
        //               decoration: BoxDecoration(color: Colors.orange,shape: BoxShape.circle),
        //               width: 10,
        //               height: 10,
        //             ),
        //             if(index!=viewModel.selectOffice.length-1)
        //             Container(height: 50,width: 1,color: Colors.orange,)
        //           ],
        //         ),
        //         Container(width: 20,),
        //         CommonText(item.officeName).onTap(() {
        //         })
        //       ],
        //     );
        //   }),
        // ).addToContainer(margin: EdgeInsets.symmetric(horizontal: 16,vertical: 15)),
        // Gaps.dividerLine,
        //
        // CommonText('选择部门').addToContainer(
        //   padding: EdgeInsets.symmetric(horizontal: 16,vertical: 15)
        // ),

        ListView.separated(
                itemBuilder: (context, index) {
                  var item = viewModel.dataList[index];
                  return officeItem(item);
                },
                separatorBuilder: (context, index) {
                  return Gaps.dividerLine;
                },
                itemCount: viewModel.dataList.length)
            .setWeight(1)
      ],
    );
  }

  Widget officeItem(OfficeListTreeData item) {
    return Row(
      children: [
        CommonText(item.officeName).setWeight(1),
        if ((item.children??[]).isNotEmpty)
          Icon(
            Icons.arrow_forward_ios_outlined,
            size: 18,
          )
      ],
    )
        .addToContainer(
            padding: EdgeInsets.symmetric(horizontal: 16, vertical: 14))
        .onTap(() {
      if ((item.children??[]).isEmpty) {
        Go().pop(result: item);
        return;
      }
      if (!viewModel.selectOffice.contains(item)) {
        setState(() {
          viewModel.selectOffice.add(item);
        });
      }
    });
  }

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      appBar: AppBar(
        title: Text('选择部门'),
        actions: [
          (vm?.selectOffice??[]).isEmpty?Container():TextButton(onPressed: (){
            setState(() {
              viewModel.selectOffice.removeLast();
            });
          }, child: Text('返回上级'))
        ],
      ),
      body: loadingContentWidget,
    );
  }

  @override
  OfficeListViewModel createViewModel() {
    return OfficeListViewModel(context);
  }

  @override
  void onRetryClick() {
    viewModel.requestRefresh(showAni: false);
  }
}
